package com.lanvideo.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.NonNull;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Web配置类
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Value("${video.upload-path:D:/lan-video/}")
    private String uploadPath;

    @Autowired
    private AuthInterceptor authInterceptor;

    /**
     * 配置静态资源映射
     */
    @Override
    public void addResourceHandlers(@NonNull ResourceHandlerRegistry registry) {
        
        // 确保路径以/结尾，并处理Windows路径分隔符
        String normalizedPath = uploadPath.replace("\\", "/");
        if (!normalizedPath.endsWith("/")) {
            normalizedPath += "/";
        }
        
        
        // 检查目录是否存在
        java.io.File uploadDir = new java.io.File(uploadPath);
        
        // 映射视频文件访问路径
        registry.addResourceHandler("/videos/**")
                .addResourceLocations("file:" + normalizedPath)
                .setCachePeriod(3600); // 缓存1小时
        
        // 映射静态资源 - 优先级高的放在前面
        registry.addResourceHandler("/css/**")
                .addResourceLocations("classpath:/static/css/")
                .setCachePeriod(0);
                
        registry.addResourceHandler("/js/**")
                .addResourceLocations("classpath:/static/js/")
                .setCachePeriod(0);
                
        registry.addResourceHandler("/admin/**")
                .addResourceLocations("classpath:/static/admin/")
                .setCachePeriod(0);
                
        registry.addResourceHandler("/teacher/**")
                .addResourceLocations("classpath:/static/teacher/")
                .setCachePeriod(0);
                
        registry.addResourceHandler("/student/**")
                .addResourceLocations("classpath:/static/student/")
                .setCachePeriod(0);
        
        // 通用静态资源映射 - 放在最后
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/")
                .setCachePeriod(0); // 开发环境不缓存
                
    }

    /**
     * 配置拦截器
     */
    @Override
    public void addInterceptors(@NonNull InterceptorRegistry registry) {
        registry.addInterceptor(authInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(
                        "/login.html",
                        "/register.html",
                        "/api/user/login",
                        "/api/user/register",
                        "/css/**",
                        "/js/**",
                        "/videos/**",
                        "/favicon.ico"
                );
    }

    /**
     * 配置视图控制器
     */
    @Override
    public void addViewControllers(@NonNull ViewControllerRegistry registry) {
        
        // 配置根路径重定向到登录页面
        registry.addRedirectViewController("/", "/login.html");
        
        // ==================== 管理员页面路由 ====================
        registry.addViewController("/admin").setViewName("forward:/admin/dashboard.html");
        registry.addViewController("/admin/").setViewName("forward:/admin/dashboard.html");
        
        // ==================== 老师页面路由 ====================
        registry.addViewController("/teacher").setViewName("forward:/teacher/dashboard.html");
        registry.addViewController("/teacher/").setViewName("forward:/teacher/dashboard.html");
        
        // ==================== 学生页面路由 ====================
        registry.addViewController("/student").setViewName("forward:/student/index.html");
        registry.addViewController("/student/").setViewName("forward:/student/index.html");
        
    }
}
